// SPDX-License-Identifier: Mulan PSL v2
/*
 * Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * This software is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

//! Request recording module
//!
//! This module provides functionality to record and replay Request messages in IPC communication,
//! primarily used for debugging, performance analysis, and regression testing.

use std::path::PathBuf;
use std::time::Instant;
use thiserror::Error;

pub mod format;
pub mod logger;
pub mod player;
pub mod serializer;

#[cfg(test)]
mod tests;

/// Recorder error types
#[derive(Debug, Error)]
pub enum RecorderError {
    #[error("I/O error: {0}")]
    Io(#[from] std::io::Error),

    #[error("serialization error: {0}")]
    Serialization(String),

    #[error("deserialization error: {0}")]
    Deserialization(String),

    #[error("file format error: {message}")]
    FormatError { message: String },

    #[error("buffer overflow: required {required}, available {available}")]
    BufferOverflow { required: usize, available: usize },

    #[error("invalid record at position {position}")]
    InvalidRecord { position: u64 },

    #[error("protobuf encoding error: {0}")]
    ProtobufEncoding(#[from] prost::EncodeError),

    #[error("protobuf decoding error: {0}")]
    ProtobufDecoding(#[from] prost::DecodeError),

    #[error("file corruption detected: {details}")]
    Corruption { details: String },
}

/// Recorder configuration (simplified version)
#[derive(Debug, Clone)]
pub struct RecorderConfig {
    /// Output file path
    pub output_path: PathBuf,
    /// Whether to enable compression
    pub compression: bool,
    /// Buffer size
    pub buffer_size: usize,
    /// Maximum file size (bytes)
    pub max_file_size: u64,
    /// Sync interval (seconds)
    pub sync_interval_secs: u64,
    /// Whether to enable checksum verification
    pub checksum_enabled: bool,
}

impl Default for RecorderConfig {
    fn default() -> Self {
        Self {
            output_path: PathBuf::from("requests.rec"),
            compression: false,
            buffer_size: 64 * 1024,            // 64KB
            max_file_size: 1024 * 1024 * 1024, // 1GB
            sync_interval_secs: 5,
            checksum_enabled: true,
        }
    }
}

/// Recorder statistics
#[derive(Debug, Default)]
pub struct RecorderStats {
    /// Total number of recorded requests
    pub total_requests: u64,
    /// Total bytes written
    pub total_bytes: u64,
    /// Flush count
    pub flush_count: u64,
    /// Last flush time
    pub last_flush: Option<Instant>,
}

// Re-export main types
pub use format::{RecordReader, RecordWriter};
pub use logger::RequestLogger;
pub use player::RequestPlayer;
pub use serializer::{
    deserialize_record, deserialize_request, serialize_record, serialize_request,
};

// Re-export protobuf generated types
pub use crate::types::xgpu_recorder::v1::{
    RecordBlock, RecordPayload, RecorderFooter, RecorderHeader, RecorderVersion, SystemContext,
    JOURNAL_MAGIC,
};
